const StringAlgorithms = require("../template")

function testStringAlgorithmsBasic() {
  const stringAlgo = new StringAlgorithms()

  console.log(
    "1. 字符串反转测试:",
    (() => {
      const tests = [
        { input: "hello", expected: "olleh" },
        { input: "world", expected: "dlrow" },
        { input: "a", expected: "a" },
        { input: "", expected: "" },
        { input: "12345", expected: "54321" },
        { input: "Hello World", expected: "dlroW olleH" }
      ]
      return tests.every(test => stringAlgo.reverseString(test.input) === test.expected)
    })()
  )

  console.log(
    "2. 字符串去重测试:",
    (() => {
      const tests = [
        { input: "hello", expected: "helo" },
        { input: "aabbcc", expected: "abc" },
        { input: "abc", expected: "abc" },
        { input: "aaaa", expected: "a" },
        { input: "", expected: "" },
        { input: "a", expected: "a" }
      ]
      return tests.every(test => stringAlgo.removeDuplicates(test.input) === test.expected)
    })()
  )

  console.log(
    "3. 字符串压缩测试:",
    (() => {
      const tests = [
        { input: "aabcccccaaa", expected: "a2b1c5a3" },
        { input: "abc", expected: "a1b1c1" },
        { input: "a", expected: "a1" },
        { input: "aa", expected: "a2" },
        { input: "", expected: "" },
        { input: "aabbcc", expected: "a2b2c2" }
      ]
      return tests.every(test => stringAlgo.compressString(test.input) === test.expected)
    })()
  )

  console.log(
    "4. 字符串解压缩测试:",
    (() => {
      const tests = [
        { input: "a2b1c5a3", expected: "aabcccccaaa" },
        { input: "a1b1c1", expected: "abc" },
        { input: "a1", expected: "a" },
        { input: "a2", expected: "aa" },
        { input: "", expected: "" },
        { input: "a2b2c2", expected: "aabbcc" }
      ]
      return tests.every(test => stringAlgo.decompressString(test.input) === test.expected)
    })()
  )

  console.log(
    "5. 字符串替换测试:",
    (() => {
      const tests = [
        { str: "hello world", oldStr: "world", newStr: "universe", expected: "hello universe" },
        { str: "hello hello", oldStr: "hello", newStr: "hi", expected: "hi hi" },
        { str: "abc", oldStr: "d", newStr: "e", expected: "abc" },
        { str: "", oldStr: "a", newStr: "b", expected: "" },
        { str: "a", oldStr: "a", newStr: "b", expected: "b" }
      ]
      return tests.every(test => stringAlgo.replaceString(test.str, test.oldStr, test.newStr) === test.expected)
    })()
  )

  console.log(
    "6. 字符串验证测试:",
    (() => {
      const tests = [
        { input: "()", expected: true },
        { input: "()[]{}", expected: true },
        { input: "(]", expected: false },
        { input: "([)]", expected: false },
        { input: "{[]}", expected: true },
        { input: "", expected: true }
      ]
      return tests.every(test => stringAlgo.isValidString(test.input) === test.expected)
    })()
  )

  console.log(
    "7. this上下文字符串测试:",
    (() => {
      const stringObj = {
        prefix: "result_",
        processString: function(str) {
          const stringAlgo = new StringAlgorithms()
          const result = stringAlgo.reverseString(str)
          return this.prefix + result
        }
      }
      return stringObj.processString("hello") === "result_olleh"
    })()
  )

  console.log(
    "8. 复杂对象this绑定测试:",
    (() => {
      const stringConfigObj = {
        config: { suffix: "_processed" },
        processWithConfig: function(str) {
          const stringAlgo = new StringAlgorithms()
          const result = stringAlgo.removeDuplicates(str)
          return result + this.config.suffix
        }
      }
      return stringConfigObj.processWithConfig("hello") === "helo_processed"
    })()
  )

  console.log(
    "9. 字符串算法一致性测试:",
    (() => {
      const testString = "aabcccccaaa"
      const compressed = stringAlgo.compressString(testString)
      const decompressed = stringAlgo.decompressString(compressed)
      return decompressed === testString
    })()
  )

  console.log(
    "10. 字符串算法边界测试:",
    (() => {
      const emptyResult = stringAlgo.reverseString("") === "" &&
                         stringAlgo.removeDuplicates("") === "" &&
                         stringAlgo.compressString("") === "" &&
                         stringAlgo.decompressString("") === "" &&
                         stringAlgo.isValidString("") === true
      
      const singleCharResult = stringAlgo.reverseString("a") === "a" &&
                              stringAlgo.removeDuplicates("a") === "a" &&
                              stringAlgo.compressString("a") === "a1" &&
                              stringAlgo.decompressString("a1") === "a"
      
      return emptyResult && singleCharResult
    })()
  )

  console.log(
    "11. 字符串算法性能测试:",
    (() => {
      const longString = "a".repeat(1000) + "b".repeat(1000)
      const start = performance.now()
      stringAlgo.reverseString(longString)
      stringAlgo.removeDuplicates(longString)
      stringAlgo.compressString(longString)
      const end = performance.now()
      return (end - start) < 1000 // 性能测试应该在1秒内完成
    })()
  )

  console.log(
    "12. 字符串算法复杂测试:",
    (() => {
      const complexString = "Hello World! 123"
      const reversed = stringAlgo.reverseString(complexString)
      const deduplicated = stringAlgo.removeDuplicates(complexString)
      const compressed = stringAlgo.compressString(complexString)
      
      return reversed === "321 !dlroW olleH" &&
             deduplicated === "Helo Wrd!123" &&
             compressed === "H1e1l2o1 1W1o1r1l1d1!1 112131"
    })()
  )
}

testStringAlgorithmsBasic()
